/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.view;

import dreamwar.controller.FeatureController;
import dreamwar.controller.GUIController;
import java.awt.event.KeyEvent;
import javax.swing.text.AttributeSet;

/**
 *
 * @author Berto_Romero
 */
public class CreateFeaturePanel extends javax.swing.JPanel {

    /**
     * Creates new form CreateFeatureJPanel
     */
    public CreateFeaturePanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        newFeaturePropertiesPanel = new javax.swing.JPanel();
        featureNameLabel = new javax.swing.JLabel();
        featureNameTextField = new javax.swing.JTextField();
        featureDescriptionLabel = new javax.swing.JLabel();
        featureDescriptionScrollPane = new javax.swing.JScrollPane();
        featureDescriptionTextArea = new javax.swing.JTextArea();
        featureCostLabel = new javax.swing.JLabel();
        featureCostTextField = new javax.swing.JTextField();
        isUnitFeatureCheckBox = new javax.swing.JCheckBox();
        continueAddingCheckBox = new javax.swing.JCheckBox();
        keypadPanel = new javax.swing.JPanel();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.columnWidths = new int[] {0};
        layout.rowHeights = new int[] {0, 5, 0};
        setLayout(layout);

        java.awt.GridBagLayout jPanel2Layout = new java.awt.GridBagLayout();
        jPanel2Layout.columnWidths = new int[] {0, 5, 0, 5, 0, 5, 0};
        jPanel2Layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0};
        newFeaturePropertiesPanel.setLayout(jPanel2Layout);

        featureNameLabel.setText("Nombre:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        newFeaturePropertiesPanel.add(featureNameLabel, gridBagConstraints);

        featureNameTextField.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        newFeaturePropertiesPanel.add(featureNameTextField, gridBagConstraints);

        featureDescriptionLabel.setText("Descripción:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
        newFeaturePropertiesPanel.add(featureDescriptionLabel, gridBagConstraints);

        featureDescriptionScrollPane.setPreferredSize(new java.awt.Dimension(292, 113));

        featureDescriptionTextArea.setColumns(20);
        featureDescriptionTextArea.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        featureDescriptionTextArea.setLineWrap(true);
        featureDescriptionTextArea.setRows(5);
        featureDescriptionTextArea.setWrapStyleWord(true);
        featureDescriptionTextArea.setPreferredSize(new java.awt.Dimension(290, 94));
        featureDescriptionScrollPane.setViewportView(featureDescriptionTextArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 5;
        newFeaturePropertiesPanel.add(featureDescriptionScrollPane, gridBagConstraints);

        featureCostLabel.setText("Coste:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        newFeaturePropertiesPanel.add(featureCostLabel, gridBagConstraints);

        featureCostTextField.setDocument(new ValidateDocument());
        featureCostTextField.setText("0");
        featureCostTextField.setPreferredSize(new java.awt.Dimension(50, 20));
        featureCostTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                featureCostTextFieldKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        newFeaturePropertiesPanel.add(featureCostTextField, gridBagConstraints);

        isUnitFeatureCheckBox.setText("Rasgo de Unidad");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        newFeaturePropertiesPanel.add(isUnitFeatureCheckBox, gridBagConstraints);

        continueAddingCheckBox.setText("Aceptar y seguir introduciendo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        newFeaturePropertiesPanel.add(continueAddingCheckBox, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(25, 25, 0, 25);
        add(newFeaturePropertiesPanel, gridBagConstraints);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 5, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        keypadPanel.setLayout(jPanel1Layout);

        acceptButton.setText("Aceptar");
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(acceptButton, gridBagConstraints);

        cancelButton.setText("Volver");
        cancelButton.setMaximumSize(new java.awt.Dimension(71, 23));
        cancelButton.setMinimumSize(new java.awt.Dimension(71, 23));
        cancelButton.setPreferredSize(new java.awt.Dimension(71, 23));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 25, 25, 25);
        add(keypadPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        createFeature();
    }//GEN-LAST:event_acceptButtonActionPerformed

    private void featureCostTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_featureCostTextFieldKeyTyped
        char c = evt.getKeyChar();
        String cost = featureCostTextField.getText().trim();
        if (c == KeyEvent.VK_MINUS) {
            if (! cost.equals("0")) {
                if (cost.startsWith("-")) {
                    featureCostTextField.setText(cost.substring(1));
                } else {
                    featureCostTextField.setText("-" + cost);
                }
            }
            evt.consume();
        } else if (c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE) {
            if (cost.matches("-?")) {
                featureCostTextField.setText("0");
                evt.consume();
            }
        } else if (cost.equals("0") && (c + "").matches("[1-9]")) {
            featureCostTextField.setText("");
        } else if (cost.isEmpty()) {
            featureCostTextField.setText("0");
        } else if (cost.matches("0+-?[1-9]+")) {
            featureCostTextField.setText(cost.replaceFirst("0+", ""));
        }
    }//GEN-LAST:event_featureCostTextFieldKeyTyped

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        GUIController.getInstance().changeMainFrameCard(GUIController.FEATURE_LIST_PANEL);
        clearFields();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void createFeature() {
        String newName = featureNameTextField.getText().trim();
        String newDescription = featureDescriptionTextArea.getText().trim();
        int newCost = (featureCostTextField.getText().trim().isEmpty() ? 0 : Integer.parseInt(featureCostTextField.getText().trim()));
        boolean isUnitFeature = isUnitFeatureCheckBox.isSelected();
        clearFields();
        if (!newName.equals("")) {
            FeatureController.getInstance().createFeature(this, newName, newDescription, newCost, isUnitFeature);
        }
        if (!continueAddingCheckBox.isSelected()) {
            GUIController.getInstance().changeMainFrameCard(GUIController.FEATURE_LIST_PANEL);
        }
    }
    
    private void clearFields() {
        featureNameTextField.setText("");
        featureCostTextField.setText("");
        featureDescriptionTextArea.setText("");
        isUnitFeatureCheckBox.setSelected(false);
    }
    
    private class ValidateDocument extends javax.swing.text.PlainDocument {
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws javax.swing.text.BadLocationException {
            String cost = featureCostTextField.getText().trim();
            if (cost.matches("-?[1-9]\\d*")) {
                str = str.replaceFirst("-", "");
            }
            if (! str.matches("-?\\d+?")) {
                str = "";
            } else if (str.matches("-?0+[1-9]+")) {
                str = str.replaceFirst("0+", "");
            }
            super.insertString(offs, str, a);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox continueAddingCheckBox;
    private javax.swing.JLabel featureCostLabel;
    private javax.swing.JTextField featureCostTextField;
    private javax.swing.JLabel featureDescriptionLabel;
    private javax.swing.JScrollPane featureDescriptionScrollPane;
    private javax.swing.JTextArea featureDescriptionTextArea;
    private javax.swing.JLabel featureNameLabel;
    private javax.swing.JTextField featureNameTextField;
    private javax.swing.JCheckBox isUnitFeatureCheckBox;
    private javax.swing.JPanel keypadPanel;
    private javax.swing.JPanel newFeaturePropertiesPanel;
    // End of variables declaration//GEN-END:variables
}
